package com.block.applet.service.impl;

import com.block.applet.service.AppletCountInfoService;
import com.block.db.entity.AppletConfigInfo;
import com.block.db.entity.AppletCountInfo;
import com.block.db.entity.ProductBaseInfo;
import com.block.db.entity.ProductOnlieInfo;
import com.block.db.mapper.AppletConfigInfoMapper;
import com.block.db.mapper.AppletCountInfoMapper;
import com.block.db.mapper.ProductOnlieInfoMapper;
import com.block.db.mapper.ProductProfitInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AppletCountInfoServiceImpl implements AppletCountInfoService {

    @Autowired
    AppletConfigInfoMapper appletConfigInfoMapper;
    @Autowired
    AppletCountInfoMapper appletCountInfoMapper;
    @Autowired
    ProductOnlieInfoMapper productOnlieInfoMapper;

    public static final String SIMPLE_TIME = "HH:mm:ss";

    @Override
    public void insertCount(String channelCode, String uid, String type) {
        AppletConfigInfo appletConfigInfo = appletConfigInfoMapper.selectAppletByChannelCode(channelCode);
        String dateInfo = LocalDate.now().toString();
        //AppletCountInfo appletCountInfo =  appletCountInfoMapper.selectAppletByUidAndDateInfo(uid,dateInfo);
        AppletCountInfo appletCountInfo = new AppletCountInfo();
            appletCountInfo.setChannelCode(channelCode);
            appletCountInfo.setUid(uid);
            appletCountInfo.setDateInfo(dateInfo);
            appletCountInfo.setOfficialName(appletConfigInfo.getGzhName());
            if ("2".equals(type)) {
                appletCountInfo.setOpen(1);
                appletCountInfoMapper.insert(appletCountInfo);
            } else if ("3".equals(type)) {
                appletCountInfo.setPress(1);
                appletCountInfoMapper.insert(appletCountInfo);
            }
    }

    @Override
    public void insertWxCount(String channelCode, String uid, String type) {
        String dateInfo = LocalDate.now().toString();
        String nowDate = LocalDate.now().toString();
        String nowTime =getNowDateStr(SIMPLE_TIME);
        List<ProductOnlieInfo> weCom = productOnlieInfoMapper.getWeCom(nowDate, nowTime);
        ProductOnlieInfo productOnlieInfo = weCom.get(0);
        AppletCountInfo appletCountInfo = new AppletCountInfo();
            appletCountInfo.setChannelCode(channelCode);
            appletCountInfo.setUid(uid);
            appletCountInfo.setDateInfo(dateInfo);
            appletCountInfo.setOfficialName(productOnlieInfo.getProductName());
            if ("4".equals(type)) {
                appletCountInfo.setOpenWeCom(1);
                appletCountInfoMapper.insert(appletCountInfo);
            } else if ("5".equals(type)) {
                appletCountInfo.setPressWeCom(1);
                appletCountInfoMapper.insert(appletCountInfo);
            }
    }

    public static String getNowDateStr(String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        return localTime;
    }

}
